<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>

  import echarts from 'echarts';
  require('echarts/theme/macarons');
  import resize from '@/views/dashboard/mixins/resize';
  import { getStaffStatusChartPie } from "@/api/site/labour/chart";

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      },
      institutionCode: {
        type: String,
        required: true
      }
    },
    data() {
      return {
        chart: null,
        data: [],
      }
    },
    watch: {
      institutionCode: {
        deep: true,
        handler(val) {
          this.institutionCode = val;
          this.setOptions();
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart();
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.setOptions();
      },
      setOptions() {
        this.data = [];
        // 加载数据
        getStaffStatusChartPie({institutionCode: this.institutionCode}).then(response => {
          this.data = response.data;
          this.chart = echarts.init(this.$el, 'macarons');
          let option = {
            tooltip: {
              trigger: 'item',
              formatter: '{a} <br/>{b} : {c} ({d}%)'
            },
            legend: {
              left: 'center',
              bottom: '10',
              data: ['待进场', '已进场', '已退场']
            },
            color: ['#C1BFBF', '#47C952', '#B52F43'],
            series: [
              {
                name: '劳务工',
                type: 'pie',
                radius: [15, 95],
                center: ['50%', '40%'],
                data: this.data,
                animationEasing: 'cubicInOut',
                animationDuration: 2600
              }
            ]
          };
          this.chart.setOption(option);
          if (response.data.length == 0) {
            this.chart.showLoading({
              text: '暂无数据',
              showSpinner: false,
              textColor: '#808080',
              maskColor: 'rgba(255, 255, 255, 1)',
              fontSize: '26px',
              fontWeight: 'bold'
            })
          } else {
            this.chart.hideLoading();
          }
        });
      }
    }
  }
</script>
